/*
 * BanBu_HintsViewCell
 * @description 本文件提供聊天界面对话界面显示系统提示消息的cell
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供聊天界面对话界面显示系统提示消息的cell
 */

#import "BanBu_HintsViewCell.h"
#import "BanBuAPIs.h"
#import "AppCommunicationManager.h"
#import "AppDataManager.h"
#import "BanBu_SmileLabelCell.h"

@implementation BanBu_HintsViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        //银行提示lable
        _hintsLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _hintsLabel.numberOfLines = 0;
        _hintsLabel.backgroundColor = [UIColor clearColor];
        _hintsLabel.textColor = [UIColor whiteColor];
        _hintsLabel.alpha = 0.5;
        _hintsLabel.textAlignment = kTextAlignmentLeft;
        _hintsLabel.hidden = YES;
        _hintsLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _hintsLabel.font = [UIFont systemFontOfSize:14];
        
        _hintsView = [[UIView alloc]initWithFrame:CGRectZero];
        _hintsView.backgroundColor = [UIColor grayColor];
        _hintsView.alpha = 0.5;
        _hintsView.layer.cornerRadius = 10;
        _hintsView.hidden = NO;
        [self addSubview:_hintsView];
        [self addSubview:_hintsLabel];
        
        if(!IS_IOS_5)
        {
            _hintsLabel1 = [[CXAHyperlinkLabel alloc]initWithFrame:CGRectZero];
            _hintsLabel1.numberOfLines = 0;
            _hintsLabel1.backgroundColor = [UIColor clearColor];
            _hintsLabel1.alpha = 0.5;
            _hintsLabel1.textAlignment = kTextAlignmentLeft;
            _hintsLabel1.hidden = NO;
            _hintsLabel1.userInteractionEnabled = YES;
            [self addSubview:_hintsLabel1];
        }

    }
    return self;
}

-(void)dealloc
{
    [_hintsLabel release],_hintsLabel = nil;
    [_hintsView release],_hintsView = nil;
    if(!IS_IOS_5)
    {
        [_hintsLabel1 release],_hintsLabel1 = nil;
    }
    [super dealloc];
}

-(void)layoutSubviews
{
    self.bkView.alpha = 0;
    self.hintsLabel.frame = HintsLabelRect;
    self.hintsLabel.center = CGPointMake(self.center.x, self.hintsLabel.center.y);
    _hintsView.frame = CGRectMake(self.hintsLabel.frame.origin.x-5,self.hintsLabel.frame.origin.y-5,self.hintsLabel.frame.size.width+10,self.hintsLabel.frame.size.height+10);
    
    if (IS_IOS_5)
    {
        self.hintsLabel.hidden = NO;
    }
    else
    {
        NSArray *URLs;
        NSArray *URLRanges;
        NSAttributedString *as = [self attributedString:&URLs URLRanges:&URLRanges];
        
        self.hintsLabel1.attributedText = as;
        
        [self.hintsLabel1 setURLs:URLs forRanges:URLRanges];
        
        self.hintsLabel1.URLClickHandler = ^(CXAHyperlinkLabel *label, NSURL *URL, NSRange range, NSArray *textRects){
            
            if ([self.delegate respondsToSelector:@selector(didGetInfo:)])
            {
                [self.delegate didGetInfo:[NSString stringWithFormat:@"%@",URL]];
            }
        };
        
        CGPoint point = self.hintsView.frame.origin;
        
        CGFloat margin = 10.;
        CGSize size = CGRectInset(CGRectMake(0, 0, self.hintsView.frame.size.width + 10, 100), margin, margin).size;
        size.height = INT16_MAX;
        size = [self.hintsLabel1 sizeThatFits:size];
        self.hintsLabel1.frame = CGRectMake(point.x+5, point.y, size.width, size.height);
        
        self.hintsLabel1.center = CGPointMake(self.hintsLabel1.center.x, self.hintsLabel1.center.y+5);
        
        self.hintsView.frame = CGRectMake(self.hintsLabel1.frame.origin.x-5,self.hintsLabel1.frame.origin.y-5,self.hintsLabel1.frame.size.width+10,self.hintsLabel1.frame.size.height+10);
    }
}


#pragma mark - privates
- (NSAttributedString *)attributedString:(NSArray *__autoreleasing *)outURLs
                               URLRanges:(NSArray *__autoreleasing *)outURLRanges
{
    NSString * HTMLText = self.hintsLabel.text;
    NSArray *URLs;
    NSArray *URLRanges;
    UIColor *color = [UIColor whiteColor];
    UIFont *font = [UIFont fontWithName:@"Baskerville" size:14.0];
    NSMutableParagraphStyle *mps = [[NSMutableParagraphStyle alloc] init];
    mps.lineSpacing = ceilf(font.pointSize * .5);
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowOffset = CGSizeMake(0, 1);
    NSString *str = [NSString stringWithHTMLText:HTMLText baseURL:[NSURL URLWithString:@""] URLs:&URLs URLRanges:&URLRanges];
    
    NSMutableAttributedString *mas = [[[NSMutableAttributedString alloc] initWithString:str attributes:@
                                       {
                                           NSForegroundColorAttributeName : color,
                                           NSFontAttributeName            : font,
                                           NSParagraphStyleAttributeName  : mps,
                                           NSShadowAttributeName          : shadow,
                                       }] autorelease];
    [shadow release];
    [URLRanges enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
        [mas addAttributes:@
         {
             NSForegroundColorAttributeName : [UIColor blueColor],
             NSUnderlineStyleAttributeName  : @(NSUnderlineStyleSingle)
         } range:[obj rangeValue]];
    }];
    
    *outURLs = URLs;
    *outURLRanges = URLRanges;
    
    return [mas copy];
}

//设置cell的各种信息
-(void)setChatCellInfo:(NSDictionary *)amsg
{
    self.avatar.hidden = YES;
    [super setChatCellInfo:amsg];
    
    self.showFrom = NO;
    [self setHintsLabelText:[amsg valueForKey:KeyContent]];
}

//设置提示信息的label
-(void)setHintsLabelText:(NSString *)text
{
    self.hintsLabel.text = text;
    CGSize size = [self.hintsLabel.text sizeWithFont:DefaultFont constrainedToSize:CGSizeMake(260-20, 1000)lineBreakMode:kTextLineBreakByWordWrapping];
    CGRect newFrame = self.hintsLabel.frame;
    
    newFrame.size.height = [BanBu_SmileLabelCell heightForText:text];
    newFrame.size.width = (size.height>DefaultHeight)?260:(size.width + 20);
    self.hintsLabel.frame = newFrame;
    self.hintsView.frame = self.hintsLabel.frame;
    
    self.bkViewWidth = self.hintsLabel.frame.size.width;
    if(self.bkViewWidth>160)
    {
        self.bkViewWidth = 160;
    }
}

-(float)heightForTextHint:(NSString *)text
{
    self.hintsLabel.text = text;
    NSArray *URLs;
    NSArray *URLRanges;
    NSAttributedString *as = [self attributedString:&URLs URLRanges:&URLRanges];
    
    self.hintsLabel1.attributedText = as;
    [self.hintsLabel1 setURLs:URLs forRanges:URLRanges];
    
    CGFloat margin = 10.;
    CGSize size = CGRectInset(CGRectMake(0, 0, 270, 100), margin, margin).size;
    size.height = INT16_MAX;
    size = [self.hintsLabel1 sizeThatFits:size];
    
    return size.height+5;
}
@end
